<?php
function remove_item_by_value($array, $val = '', $preserve_keys = true) {
	if (empty($array) || !is_array($array)) return false;
	if (!in_array($val, $array)) return $array;

	foreach($array as $key => $value) {
		if ($value == $val) unset($array[$key]);
	}
	return ($preserve_keys === true) ? $array : array_values($array);
}


function cropModuleName($class)
{
    $parts = explode("\\", $class);
    return $parts[2];
}

function classToPath($class)
{   
    if($class[0] == '\\') $class = substr($class, 1);
    return str_replace('\\', '/', $class);
}


function loadLanguageFile($class, $filename)
{
    // / found
    if(strpos($filename, "/") !== false)
    {
        // Language file
        $langFile = substr($filename, 0, strrpos($filename, "/")) . "/lang/" . substr($filename, strrpos($filename, "/") + 1);
    }
    else{
        // not found
        $langFile = "lang/" . $filename;
    }
    
    // append language
    $langFile .=  "_" .  \Cyfy\Language :: getActive() . ".php";
    if(file_exists($langFile))
    {
        include_once($langFile);
        
        // loads data
        if($lang) $class :: addLanguageData(\Cyfy\Language :: getActive(), $lang);

        // load custom name
        if($name)
        {
            $class :: setName($name);
        }
        return true;
    }
    return false;
}

// autoload function   
function __autoload($class)
{   
    $filename = classToPath($class);   
    if(!file_exists($filename . '.php'))
    {
        throw new Exception("Unable to load $filename.");
    }
    include_once($filename . '.php');   
    
    loadLanguageFile($class, $filename);
}   

/**	
*
* Delete a file or recursively delete a directory
*
* @param string $str Path to file or directory
* @author Eddy Vlad @ php.net (24-Nov-2009 04:59)
*/

function recursiveDelete($str){
    if(is_file($str)){
        return @unlink($str);
    }
    elseif(is_dir($str)){
        $scan = glob(rtrim($str,'/').'/*');
        foreach($scan as $index=>$path){
            recursiveDelete($path);
        }
        return @rmdir($str);
    }
}

// replaces links in text with http://...
function textWithLinks($text){
    return preg_replace("/http:\/\/([-a-zA-Z0-9@:%_\+.~#?&\/=]+)/", "<a href=\"http://$1\">http://$1</a>", $text);
}

// hides email
function emailHide($email)
{
    return strtr(substr($email, 0, strrpos($email, ".")) . "&nbsp;&nbsp;&nbsp;" . substr($email, strrpos($email, ".") + 1), array("@" => "&nbsp;&nbsp;&nbsp;"));
}

// returnet formatted filesize
function filesizeFormated($size){
    $units = array("B", "kB", "MB", "TB");
    $unit = 0;
    while($size > 1024 && $unit <= 3){
        $size = round($size / 1024, 2);    
        $unit++;
    }
    return $size . " " . $units[$unit];
}

//131 dot php at cloudyks dot org
//20-Mar-2008 01:34 
//rp like, working with absolute/relative path & a little bit shorter :p 

function rp($path) { 
    $out=array(); 
    foreach(explode('/', $path) as $i=>$fold){ 
        if ($fold=='' || $fold=='.') continue; 
        if ($fold=='..' && $i>0 && end($out)!='..') array_pop($out); 
        else $out[]= $fold; 
    } return ($path{0}=='/'?'/':'').join('/', $out); 
} 

//david at ordal dot com
//03-Aug-2009 07:24 
//An improvement on the recursive chmod from nicolas@infosoft.... in July 2008. This is a little shorter and should handle all types of linux filesystem files.

function chmodr($path, $filemode) { 
    if (!is_dir($path)) 
    return @chmod($path, $filemode); 

    $dh = opendir($path); 
    while (($file = readdir($dh)) !== false) { 
        if($file != '.' && $file != '..') { 
            $fullpath = $path.'/'.$file; 
            if(is_link($fullpath)) 
            return FALSE; 
            elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode)) 
            return FALSE; 
            elseif(!@chmodr($fullpath, $filemode)) 
            return FALSE; 
        } 
    } 

    closedir($dh); 

    if(@chmod($path, $filemode)) 
    return TRUE; 
    else 
    return FALSE; 
}

// custom pass generator
function generatePassword($length=5, $strength=4) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength & 1) {
        $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength & 2) {
        $vowels .= "AEUY";
    }
    if ($strength & 4) {
        $consonants .= '23456789';
    }
    if ($strength & 8) {
        $consonants .= '@#$%';
    }
    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}



// UTF8 strlen
function strlenUTF8($str){
    $i = 0;
    $count = 0;
    $len = strlen ($str);
    while ($i < $len)
    {
        $chr = ord ($str[$i]);
        $count++;
        $i++;
        if ($i >= $len)
        break;

        if ($chr & 0x80)
        {
            $chr <<= 1;
            while ($chr & 0x80)
            {
                $i++;
                $chr <<= 1;
            }
        }
    }
    return $count;
}


//text to code
function text2Code($text){
    static $tbl = array("\xc3\xa1"=>"a","\xc3\xa4"=>"a","\xc4\x8d"=>"c","\xc4\x8f"=>"d","\xc3\xa9"=>"e","\xc4\x9b"=>"e","\xc3\xad"=>"i","\xc4\xbe"=>"l","\xc4\xba"=>"l","\xc5\x88"=>"n","\xc3\xb3"=>"o","\xc3\xb6"=>"o","\xc5\x91"=>"o","\xc3\xb4"=>"o","\xc5\x99"=>"r","\xc5\x95"=>"r","\xc5\xa1"=>"s","\xc5\xa5"=>"t","\xc3\xba"=>"u","\xc5\xaf"=>"u","\xc3\xbc"=>"u","\xc5\xb1"=>"u","\xc3\xbd"=>"y","\xc5\xbe"=>"z","\xc3\x81"=>"A","\xc3\x84"=>"A","\xc4\x8c"=>"C","\xc4\x8e"=>"D","\xc3\x89"=>"E","\xc4\x9a"=>"E","\xc3\x8d"=>"I","\xc4\xbd"=>"L","\xc4\xb9"=>"L","\xc5\x87"=>"N","\xc3\x93"=>"O","\xc3\x96"=>"O","\xc5\x90"=>"O","\xc3\x94"=>"O","\xc5\x98"=>"R","\xc5\x94"=>"R","\xc5\xa0"=>"S","\xc5\xa4"=>"T","\xc3\x9a"=>"U","\xc5\xae"=>"U","\xc3\x9c"=>"U","\xc5\xb0"=>"U","\xc3\x9d"=>"Y","\xc5\xbd"=>"Z");
    $text = strtr($text, $tbl);

    //lowercase
    $text = utf8_decode($text);
    $text = strtolower($text);
    $text = utf8_encode($text);

    //dalsi znaky  
    $input = array(" ", "+", "&", "@", "(", ")","?");
    $output = array("-", "-plus-", "-and-", "-at-", "_", "_", "_");
    $text = str_replace($input, $output, $text);

    return $text;
}


function implode_wrapped($before, $after, $glue, $array)
{
    $output = '';
    foreach($array as $item)
    {
        $output .= $before . $item . $after . $glue;
    }

    return substr($output, 0, -strlen($glue));
}
